﻿#region Copyright © 2009 by Bj Rollison
/// <copyright> Copyright © 2009 by Bj Rollison
/// This source is subject to the Microsoft Public License (Ms-PL).
/// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
/// All other rights reserved.
/// </copyright>
#endregion

namespace TestingMentor.StatisticalSampleSizeCalculator
{
  using System;
  using System.Globalization;
  using System.Windows.Forms;

  /// <summary>
  /// Initializes a new instance of the Form1 class
  /// </summary>
  internal partial class Form1 : Form
  {
    #region PUBLIC METHODS
    /// <summary>
    /// Initialize form
    /// </summary>
    public Form1()
    {
      this.InitializeComponent();
    }
    #endregion PUBLIC METHODS

    #region PRIVATE METHODS
    /// <summary>
    /// Event handler that validates the standard deviation input value and sets
    /// the StandardDeviation property in the Calculator class
    /// </summary>
    private void StdDeviationOnLeave(object sender, EventArgs e)
    {
      double standardDeviation;
      if (!HelperClass.IsNumber(
        this.tbxStdDeviation.Text, out standardDeviation) ||
        standardDeviation < .001 || standardDeviation > .5)
      {
        HelperClass.ShowMsgBox(Properties.Resources.StdDevInputErrorMsg);
        this.tbxStdDeviation.SelectAll();
        this.tbxStdDeviation.Focus();
      }
      else
      {
        Calculator.SetStandardDeviation = standardDeviation;
      }
    }

    /// <summary>
    /// Event handler that validates the Population input value and sets the 
    /// Population property in the Calculator class
    /// </summary>
    private void PopulationTextOnLeave(object sender, EventArgs e)
    {
      double population;
      if (!HelperClass.IsNumber(this.tbxPopulation.Text, out population) ||
        population < 1)
      {
        HelperClass.ShowMsgBox(Properties.Resources.PopulationInputErrorMsg);
        this.tbxPopulation.SelectAll();
        this.tbxPopulation.Focus();
      }
      else
      {
        Calculator.SetPopulationSize = (int)population;
      }
    }

    /// <summary>
    /// Event handler that sets the confidence level property in the Calculator
    /// class if the confidence level value is changed. Default value is 95%.
    /// </summary>
    private void ConfidenceLevelOnLeave(object sender, EventArgs e)
    {
      // Default value is 95%
      double confidenceLevelValue = 1.96;

      // 80% confidence level
      if (this.rbnConfidence80.Checked)
      {
        confidenceLevelValue = 1.28;
      }

      // 90% confidence level
      if (this.rbnConfidence90.Checked)
      {
        confidenceLevelValue = 1.64;
      }

      // 99% confidence level
      if (this.rbnConfidence99.Checked)
      {
        confidenceLevelValue = 2.58;
      }

      Calculator.SetConfidenceLevel = confidenceLevelValue;
    }

    /// <summary>
    /// Event handler that sets the sampling error property in the calculator
    /// class if the sampling error value is changed. Default is +/-5%.
    /// </summary>
    private void SamplingErrorOnLeave(object sender, EventArgs e)
    {
      // Default samling error is +/- 5%
      double sampleErrorValue = .05;

      // Sampling error +/- 3%
      if (this.rbnPrecision3.Checked)
      {
        sampleErrorValue = .03;
      }

      // Sampling error +/- 7%
      if (this.rbnPrecision7.Checked)
      {
        sampleErrorValue = .07;
      }

      // Sampling error +/- 10%
      if (this.rbnPrecision10.Checked)
      {
        sampleErrorValue = .1;
      }

      Calculator.SetSamplingErrorRate = sampleErrorValue;
    }

    /// <summary>
    /// Calculate button event handler that calls GetSampleSize property in
    /// calculator class and displays the result to the sample size textbox
    /// </summary>
    private void CalculateButtonOnClick(object sender, EventArgs e)
    {
      this.tbxSampleSize.Text = 
        Calculator.GetSampleSize.ToString(CultureInfo.InvariantCulture);
    }

    /// <summary>
    /// Close button event handler to exit the application
    /// </summary>
    private void CloseButtonOnClick(object sender, EventArgs e)
    {
      Application.Exit();
    }
    #endregion PRIVATE METHODS
  }
}
